package cn.itshare.junit.xmltest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

import com.share.commons.util.LoadXmlUtil;

/**
 * 说明：读取后台树状菜单XML，添加到集合中 <br/>
 * 作者：杨浩泉 <br/>
 * 日期：2010-7-18
 */
public class InitBackSysTree {
	
	private static final String PATH = "D:/Workspaces8.5/share/share/test/cn/itshare/junit/xmltest/back_init_tree.xml";

	private static LinkedHashMap<String, List<BackSysTreeVo>> treeMap = new LinkedHashMap<String, List<BackSysTreeVo>>();

	public static LinkedHashMap<String, List<BackSysTreeVo>> intiSysTree() {

		Document doc = LoadXmlUtil.getDocument(PATH);

		Element element = doc.getRootElement();

		List<?> child = element.getChildren();

		for (int i = 0; i < child.size(); i++) {

			List<BackSysTreeVo> listVal = new ArrayList<BackSysTreeVo>();

			Element elt1 = (Element) child.get(i);

			BackSysTreeVo bst = new BackSysTreeVo();
			bst.setTreeNo(elt1.getChildText("treeNo"));
			bst.setTreeName(elt1.getChildText("treeName"));
			bst.setTreeParent(elt1.getChildText("parentNo"));
			bst.setIsleaf(elt1.getChildText("treeLeaf"));
			listVal.add(bst);

			Element childs = elt1.getChild("childer");
			if (!childs.getChildren().isEmpty()) {
				loadChilds(childs.getChildren(), elt1.getChildText("treeNo"), 0, listVal);
			}
			treeMap.put(elt1.getChildText("treeName"), listVal);
		}
		return treeMap ;
	}

	/**
	 * 递归读取
	 * 
	 * @param list
	 * @param pid
	 * @param level
	 */
	public static void loadChilds(List<?> list, String pid, int level, List<BackSysTreeVo> listVal) {
		for (int i = 0; i < list.size(); i++) {

			Element elt = (Element) list.get(i);

			if (pid.equals(elt.getChildText("parentNo"))) {

				BackSysTreeVo bst = new BackSysTreeVo();
				bst.setTreeNo(elt.getChildText("treeNo"));
				bst.setTreeName(elt.getChildText("treeName"));
				bst.setTreeUrl(elt.getChildText("treeUrl"));
				bst.setTreeParent(elt.getChildText("parentNo"));
				bst.setIsleaf(elt.getChildText("treeLeaf"));
				listVal.add(bst);

				loadChilds(list, elt.getChildText("treeNo"), level + 1, listVal);
			}
		}
	}

	public static void main(String[] args) {
		testIntiSysTree();

		System.out.println("************************");

		for (String key : intiSysTree().keySet()) {
			System.out.println("【树根:" + key + "】");
			List<BackSysTreeVo> treeMapValue = treeMap.get(key);

			for (Iterator<BackSysTreeVo> iterator = treeMapValue.iterator(); iterator.hasNext();) {
				BackSysTreeVo bst = (BackSysTreeVo) iterator.next();

				System.out.println("\t" + bst.getTreeNo() + " " + bst.getTreeName() + " " + bst.getIsleaf() + " " + bst.getTreeParent());
			}
		}
	}
	
	
	
	
	
	
	
	
	
	
	/**
	 * 测试
	 */
	public static void testIntiSysTree() {

		Document doc = LoadXmlUtil.getDocument(PATH);

		Element element = doc.getRootElement();

		System.out.println("【根元素:" + element.getName() + "】");

		List<?> child = element.getChildren();

		for (int i = 0; i < child.size(); i++) {
			Element elt1 = (Element) child.get(i);
			System.out.println("一级元素：" + elt1.getName());
			System.out.println("\t" + elt1.getChildText("treeNo") + "  " + elt1.getChildText("treeName") + "  " + elt1.getChildText("getChildText"));
			Element childs = elt1.getChild("childer");
			if (!childs.getChildren().isEmpty()) {
				testLoadChilds(childs.getChildren(), elt1.getChildText("treeNo"), 0);
			}
		}
	}

	/**
	 * 递归读取
	 * 
	 * @param list
	 * @param pid
	 * @param level
	 */

	public static void testLoadChilds(List<?> list, String pid, int level) {

		String parstr = "";
		for (int j = 0; j < level; j++) {
			parstr += "   ";
		}

		for (int i = 0; i < list.size(); i++) {

			Element elt = (Element) list.get(i);

			if (pid.equals(elt.getChildText("parentNo"))) {
				System.out.println("\t\t" + parstr + elt.getChildText("treeNo") + "--" + elt.getChildText("treeName") + "--" + elt.getChildText("parentNo"));
				testLoadChilds(list, elt.getChildText("treeNo"), level + 1);

			}
		}
	}

}
